/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmInfoConnection.java
 *
 * Created on 2011-05-18, 16:52:10
 */

package pckGestionHoraire;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import pckSQL.SQL_Connection;
import pckTaxe.IniFile;

/**
 *
 * @author Portable-Maison
 */
public class FrmInfoConnection extends javax.swing.JFrame {

    IniFile laConfig = new IniFile();

    /** Creates new form FrmInfoConnection */
    public FrmInfoConnection() {
        initComponents();
        positionnerFenetre();
        chargerInformation();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel2 = new javax.swing.JPanel();
        btnSauver = new javax.swing.JButton();
        btnTestConnect = new javax.swing.JButton();
        btnFermer = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txfAdresse = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txfPort = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txfNomTable = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Information connection");
        setMinimumSize(new java.awt.Dimension(400, 105));
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        btnSauver.setMnemonic('S');
        btnSauver.setText("Sauvegarder la configuration");
        btnSauver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSauverActionPerformed(evt);
            }
        });
        jPanel2.add(btnSauver);

        btnTestConnect.setMnemonic('T');
        btnTestConnect.setText("Tester la connexion");
        btnTestConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTestConnectActionPerformed(evt);
            }
        });
        jPanel2.add(btnTestConnect);

        btnFermer.setMnemonic('F');
        btnFermer.setText("Fermer");
        btnFermer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFermerActionPerformed(evt);
            }
        });
        jPanel2.add(btnFermer);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 1;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        getContentPane().add(jPanel2, gridBagConstraints);

        jPanel1.setMinimumSize(new java.awt.Dimension(200, 100));
        jPanel1.setLayout(new java.awt.GridLayout(3, 2, 5, 10));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Adresse du serveur :");
        jPanel1.add(jLabel1);
        jPanel1.add(txfAdresse);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Port :");
        jPanel1.add(jLabel2);
        jPanel1.add(txfPort);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Nom de la table :");
        jPanel1.add(jLabel3);
        jPanel1.add(txfNomTable);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 1;
        gridBagConstraints.ipady = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        getContentPane().add(jPanel1, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnFermerActionPerformed (java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnFermerActionPerformed
    {//GEN-HEADEREND:event_btnFermerActionPerformed
    setVisible(false);
    }//GEN-LAST:event_btnFermerActionPerformed

    private void btnSauverActionPerformed (java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnSauverActionPerformed
    {//GEN-HEADEREND:event_btnSauverActionPerformed
    IniFile.ajouterInstance ("Adresse", txfAdresse.getText ().trim ());
    IniFile.ajouterInstance ("Port",txfPort.getText ().trim ());
    IniFile.ajouterInstance ("DB_Name", txfNomTable.getText ().trim ());

    IniFile.enregistrerFichier ();
    setVisible(false);
    }//GEN-LAST:event_btnSauverActionPerformed

    private void btnTestConnectActionPerformed (java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnTestConnectActionPerformed
    {//GEN-HEADEREND:event_btnTestConnectActionPerformed
    String lsMessage = "";
    if (!SQL_Connection.testerConnection (txfNomTable.getText(), txfAdresse.getText ()
                                         , "Info", "Info"))
    {
        lsMessage = "La connexion à échoue. Veuillez vérifier vos paramètre ou " +
                    "contacter l'Administrateur du système.";
    }
    else
    {
        lsMessage = "La connexion à réussi.";
    }
    JOptionPane.showMessageDialog (this, lsMessage);
    }//GEN-LAST:event_btnTestConnectActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmInfoConnection().setVisible(true);
            }
        });
    }

    public void chargerInformation()
    {
    IniFile.lectureFichier ();
    if (IniFile.getSize () > 0)
        {
        txfAdresse.setText (IniFile.getValeurSection (0));
        txfPort.setText (IniFile.getValeurSection (1));
        txfNomTable.setText (IniFile.getValeurSection (2));
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFermer;
    private javax.swing.JButton btnSauver;
    private javax.swing.JButton btnTestConnect;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txfAdresse;
    private javax.swing.JTextField txfNomTable;
    private javax.swing.JTextField txfPort;
    // End of variables declaration//GEN-END:variables

    private void positionnerFenetre ()
    {
        Dimension laTailleEcran =
                          Toolkit.getDefaultToolkit ().getScreenSize ();
        Dimension laTailleFenetre = this.getSize ();
        if (laTailleFenetre.height > laTailleEcran.height)
        {
            laTailleFenetre.height = laTailleEcran.height;
        }
        if (laTailleFenetre.width > laTailleEcran.width)
        {
            laTailleFenetre.width = laTailleEcran.width;
        }
        this.setLocation((laTailleEcran.width - laTailleFenetre.width) / 2,
                        (laTailleEcran.height - laTailleFenetre.height) / 2);
    }

}
